# -*- coding:utf-8 -*-

# @File     :tasks
# @Author   :Yupeng
# @Date     :2018/11/13 11:16

from apps.search.core.es_handler import ElasticsearchManager
from apps.storage.models.data import DataMeta
from django.db.models import Q
from celery import shared_task


@shared_task
def import_to_es(meta_id_or_list=None):
    if meta_id_or_list is None:
        meta_id_or_list = [x.id for x in
                           DataMeta.objects.filter(Q(es_record=None) | Q(es_record__status="FAILED")).only(
                               'id')]
    if isinstance(meta_id_or_list, int):
        meta_id_or_list = [meta_id_or_list]

    ElasticsearchManager.insert(meta_id_or_list)
